package com.gitee.l0km.codegen.decorator;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import com.gitee.l0km.com4j.basex.ReflectionUtils;
import com.google.common.base.Predicate;

/**
 * 用户定义public方法(非Object的方法)过滤器
 * @author guyadong
 *
 */
public class UserPublicMethodFilter implements Predicate<Method>{
	private final boolean excludeStatic;
	private final boolean excludeGeneric;
	private final boolean excludeDeprecated;
	public UserPublicMethodFilter() {
		this(true,false, true);
	}
	/**
	 * @param excludeStatic 是否排除静态方法
	 * @param excludeGeneric 是否排除泛型方法
	 * @param excludeDeprecated 是否{@link Deprecated}注释的排除废弃方法
	 */
	public UserPublicMethodFilter(boolean excludeStatic,boolean excludeGeneric, boolean excludeDeprecated) {
		this.excludeStatic = excludeStatic;
		this.excludeGeneric = excludeGeneric;
		this.excludeDeprecated = excludeDeprecated;
	}

	/**
	 * 判断是否为泛型方法
	 * @param input
	 */
	private static boolean isGenericMethod(Method input){
		return input.getTypeParameters().length > 0;
	}
	/**
	 * 判断是否为废弃方法
	 * @param input
	 */
	private static boolean isDeprecated(Method input){
		return input.getAnnotation(Deprecated.class) != null;
	}
	@Override
	public boolean apply(Method input) {
		if(input == null){
			return false;
		}
		
		int modifier = input.getModifiers();
		if(!Modifier.isPublic(modifier)){
			return false;
		}
		if(excludeStatic && Modifier.isStatic(modifier)){
			return false;
		}
		if(excludeGeneric && isGenericMethod(input)){
			return false;
		}
		if(excludeDeprecated && isDeprecated(input)){
			return false;
		}
		if(Object.class.equals(input.getDeclaringClass())){
			return false;
		}
		if(ReflectionUtils.isOverrideObjectMethod(input)){
			return false;
		}
		if(ReflectionUtils.isOverrideShadow(input)){
			return false;
		}
		return true;
	}

}
